__author__ = 'Amin'
print("Initializing...")
import re

print "Kernel Version 0.1 Unstable"
print "This program developed by Amin Mahpour"

my_file_name = "GSE17708_family.soft"

def read_file(file_name, mem=False):
	print "Opening the file..."

	for i in open(file_name, "r"):
		yield i.strip("\n")
	del(file)


def get_sample_id():
	samples = 0
	global list_of_samples

	for i in read_file(my_file_name):
		a = re.search("!Series_sample_id\s\=\s(\w+)", i)
		if a:
			isample = a.group(1)
			list_of_samples.append(isample)
			samples += 1
		else:
			if samples > 0: break
	del (samples)


def get(e):
	print "this will fetch data for ", e
	show = False
	for i in read_file(my_file_name):
		i = i.strip("\n")
		if re.search("\^SAMPLE\s=\s%s" % format(e), i):
			print "Entry found!"
			show = True

		if re.search("\!sample_table_end", i):
			if show == True: break
			show = False

		if show == True:
			info = re.search("!Sample_title = (.+)", i)
			if info:
				print(info.group(1))

			del (info)
		#print (i)


exe = True
cmds = {"samples": "get_sample_id()"}
funs = {"get\s(\w+)": "get({0})"}

while exe == True:
	valid = False
	inp = raw_input(">>")
	for i in cmds.keys():
		ask = re.search(i, inp)
		if ask:
			exec (cmds[i])
			valid = True

	for i in funs.keys():
		ask = re.search(i, inp)
		if ask:
			valid = True
			for j in ask.groups():
				exec(str(funs[i]).format("j"))

	if valid == False:
		print "Command not defined!"

	if inp == "exit":
		exe = False